//
//  NSUserDefaultsViewController.m
//  项目二
//
//  Created by rimi on 16/5/23.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "NSUserDefaultsViewController.h"

@interface NSUserDefaultsViewController ()

@property (nonatomic,strong)UITextField *nameText;
@property (nonatomic,strong)UITextField *ageTeXT;
@property (nonatomic,strong)UITextField *numberID;


@property (nonatomic,strong)UITextField *name;
@property (nonatomic,strong)UITextField *age;
@property (nonatomic,strong)UITextField *num;

@property (nonatomic,strong)NSDictionary *dic;
@property (nonatomic,strong)NSMutableArray *array;
@end

@implementation NSUserDefaultsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _dic = [NSDictionary dictionary];
    _array =[NSMutableArray array];
    [self initUIInterface1];
    [self initUIInterface2];
}

-(void)initUIInterface1{
    _nameText = [[UITextField alloc]initWithFrame:CRM(50, 100, 80, 40)];
    _nameText.placeholder = @"存入姓名";
    _nameText.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_nameText];
    
    _ageTeXT = [[UITextField alloc]initWithFrame:CRM(50, 150, 80, 40)];
    _ageTeXT.placeholder = @"存入年龄";
    _ageTeXT.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_ageTeXT];
    
    _numberID = [[UITextField alloc]initWithFrame:CRM(50, 200, 80, 40)];
    _numberID.placeholder = @"存入学号";
    _numberID.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_numberID];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CRM(50, 250, 50, 50);
    [button1 setTitle:@"存入" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor redColor];
    [button1 addTarget:self action:@selector(button1T:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];

}

-(void)initUIInterface2{
    _name = [[UITextField alloc]initWithFrame:CRM(50, 300, 80, 40)];
    _name.placeholder = @"取出姓名";
    _name.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_name];
    
    _age = [[UITextField alloc]initWithFrame:CRM(50, 350, 80, 40)];
    _age.placeholder = @"取出年龄";
    _age.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_age];
    
    _num = [[UITextField alloc]initWithFrame:CRM(50, 400, 80, 40)];
    _num.placeholder = @"取出学号";
    _num.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_num];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CRM(50, 450, 50, 50);
    [button2 setTitle:@"取出" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor redColor];
    [button2 addTarget:self action:@selector(button2T:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];

}
#pragma mark - button
/**保存*/
-(void)button1T:(UIButton *)sender{
//    _dic =@{@"name":_nameText.text,@"age":_ageTeXT.text,@"numberID":_numberID.text};
//    [_array  addObject:_dic];
//    NSArray *arrayData = [_array copy];
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    [user setObject:arrayData forKey:@"只能存不可变的"];
//    NSLog( @"arrayData = %@",arrayData);
    _dic = @{@"name":_nameText.text,@"age":_ageTeXT.text,@"numberID":_numberID.text};
    [_array addObject:_dic];
    
    NSArray *arrayData = [_array copy];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:arrayData forKey:@"只能存不可变的"];
    NSLog(@"arrayData = %@",arrayData);
}

-(void)button2T:(UIButton *)sender{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSArray *array = [NSArray arrayWithArray:[user objectForKey:@"只能存不可变的"]];
    NSLog(@"取出 = %@",array);
    for (NSDictionary *dic in array) {
        NSLog(@" dic = %@",dic);
        _name.text = dic[@"name"];
        _age.text = dic[@"age"];
        _num.text = dic[@"numberID"];
    }
    
}
@end
